const moment = require('moment');
const path = require("path");
const fs = require('fs');

//记得传入next参数
let Mylog = function (request, response, next) {

	request.on("end", function () {
		//判断地址后缀是否是js文件 是就跳过
		//不知道为什么axios.js会访问一个map结尾的地址也会生成log
		if (path.extname(request.path) != ".js"&&path.extname(request.path)!=".map") {
			//获取访问时间
			let nowTime = moment().format("YYYY-MM-DD HH:mm:ss");
			//获取客户端ip
			let userip = request.ip;
			//获取客户端浏览器信息
			let userAgent = request.headers['user-agent'];
			//获取客户端访问方法
			let userMethod = request.method;
			//获取当天日期
			let toDay = moment().format("YYYY-MM-DD");

			fs.appendFile(`log\\${toDay}.txt`, `${nowTime}-${userip}-${userMethod}-${userAgent}` + "\r\n", err => {
				console.log(err);
			})
			console.log("Mylog")
			next();
		}else{
			next();
		}
	})

	request.on("data", (buffer) => {
		console.log(buffer);
	})

}

module.exports = Mylog;